/*
 * LotteryOpt.java
 *
 * Created on Aug 18, 2009, 10:17:14 PM
 */

package dialog;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Justin
 */
public class LotteryOpt extends javax.swing.JDialog {
	/** The CSV file for the lottery game. */
	String file = "";
	/** Whether we show results during the game. */
	boolean showResults;

	/** Creates new form LotteryOpt */
    public LotteryOpt(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
		file = getFileFromFileChooser();
		showResults = false;
		updateFileLabel();
    }

	/**
	 * Updates the label showing what file we've currently selected.
	 */
	private void updateFileLabel() {
		if (file != "" && file != null) {
			fileLabel.setText (file);
		}
	}

	/**
	 * Opens a file chooser dialog, and returns the name of the file to open.
	 */
	private String getFileFromFileChooser() {
		JFileChooser fileChooser = new JFileChooser("./lotteries/");
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.addChoosableFileFilter (new CSVFilter());

        fileChooser.showOpenDialog(this);
        if(fileChooser.getSelectedFile() != null)
            return fileChooser.getSelectedFile().getName();
		else
			return null;
	}

	/**
	 * Filter for CSV files for file chooser.
	 */
	class CSVFilter extends javax.swing.filechooser.FileFilter {
		public boolean accept(File file) {
			String filename = file.getName();
			return filename.endsWith(".csv");
		}

		public String getDescription() {
			return "Comma Separated Values (*.csv)";
		}
	}

	/**
	 * Returns parameter list for config screen.
	 *
	 * @return parameters.
	 */
	public String getParamsList()
    {
		String numRepetitions = numRepetitionsField.getText();
        return numRepetitions + "," + file + "," + showResults;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chooseFileButton = new javax.swing.JButton();
        fileLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        numRepetitionsField = new javax.swing.JTextField();
        showResultsBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        chooseFileButton.setFont(new java.awt.Font("Tahoma", 0, 12));
        chooseFileButton.setText("Choose a different file.");
        chooseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileButtonActionPerformed(evt);
            }
        });

        fileLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        fileLabel.setText("No file chosen yet.");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Parameters for Lottery");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("No. of Game Repetitions:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Lottery file:");

        submitButton.setText("OK");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        numRepetitionsField.setFont(new java.awt.Font("Tahoma", 0, 12));
        numRepetitionsField.setText("1");

        showResultsBox.setFont(new java.awt.Font("Tahoma", 0, 12));
        showResultsBox.setText("Show results");
        showResultsBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                showResultsBoxItemStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(176, 176, 176)
                        .add(submitButton))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(67, 67, 67)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(chooseFileButton)
                                    .add(fileLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)))
                            .add(layout.createSequentialGroup()
                                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 201, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(numRepetitionsField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(29, 29, 29)
                                .add(jLabel2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 298, Short.MAX_VALUE))))
                    .add(layout.createSequentialGroup()
                        .add(131, 131, 131)
                        .add(showResultsBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 134, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .add(jLabel1)
                .add(28, 28, 28)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(numRepetitionsField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fileLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(chooseFileButton)
                .add(26, 26, 26)
                .add(showResultsBox)
                .add(36, 36, 36)
                .add(submitButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void chooseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileButtonActionPerformed
		file = getFileFromFileChooser();
		updateFileLabel();
}//GEN-LAST:event_chooseFileButtonActionPerformed

	private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
		if (file != null && file != "")
			this.setVisible(false);
		else {
			JOptionPane.showMessageDialog(this, "Choose a file first.",
					"No file selected.",
					JOptionPane.ERROR_MESSAGE);
		}
}//GEN-LAST:event_submitButtonActionPerformed

	private void showResultsBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_showResultsBoxItemStateChanged
		Object source = evt.getItemSelectable();
		if (source == showResultsBox) {
			showResults = true;
		}

		if (evt.getStateChange() == java.awt.event.ItemEvent.DESELECTED) {
			showResults = false;
		}
	}//GEN-LAST:event_showResultsBoxItemStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                LotteryOpt dialog = new LotteryOpt(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseFileButton;
    private javax.swing.JLabel fileLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField numRepetitionsField;
    private javax.swing.JCheckBox showResultsBox;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables

}
